/*
 * TacticaGoldenLions.java
 *
 * Created on 18 de marzo de 2007, 23:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package futbol.tacticas.concursantes2007.GoldenLions;
//import futbol.*;
import futbol.tacticas.*;
import futbol.tacticas.concursantes.Tactica;

import java.awt.Color;
import java.awt.Point;
import java.util.Vector;
/**
 *
 * @author David
 */
public class TacticaGoldenLions implements Tactica {
    private int count = 0;
    private int nGolesMios=0;
    private int nGolesContrario=0;
    private double arriesgar = 0.5;
    private double speedBall = 0.0;
    private int balonX;
    private double Y;
    private Point lastBall = new Point(0,0);
    private Point balon = new Point(0,0);
    
    private boolean ballToGoal = false;
    private int lines[][] = {
        {-165,  0},
        {-120, 50},
        {-120, 50},
        {-120, 50},
        {-120, 50},
        {-70,100},
        {-70,100},
        {-70,100},
        {-70,100},
        {   0,160},
        {   0,160},        
    };
    
    private int nValores[][]={
        {-152,0,5},
        {-100,100,6},
        {-120,30,6},
        {-120,-30,6},
        {-100,-100,6},
        {20,100,5},
        {-20,30,5},
        {-20,-30,7},
        {20,-100,6},
        {120,30,7},
        {100,-60,7}
    };

    public String getNombre(){
        return "Golden Lions";
    }
    
    public Color getColor1(){
        return new Color(255,255,0);
    }
    
    public Color getColor2(){
        return new Color(255,192,0);
    }
    
    public Color getColorNumero(){
        return new Color(255,80,0);
    }
    
    public int getFuerza(int n){
        return nValores[n][2];
    }
    public int getXInicial(int n){
        return nValores[n][0];
    }
    
    public int getYInicial(int n){
        return nValores[n][1];
    }
  
    private void updateInfoBalon(Point balon,boolean esGol){
        if ( !esGol ){
            double dx = balon.getX() - lastBall.getX();
            double dy = balon.getY() - lastBall.getY();
            speedBall = Math.sqrt((dx*dx) + (dy*dy))-1.0;
            if ( speedBall < 0.0 )speedBall = 0;
            else speedBall = Math.round(speedBall);
            if ( dx < 0 ){
                Y = lastBall.getY() + ((dy)*(nValores[0][0]-lastBall.getX()))/dx;
                if ( Y < 30 && Y > -30 ) {
                    ballToGoal = true;
                }
                else ballToGoal = false;
            }
            else ballToGoal = false;
        }
        else {
            speedBall = 0;
        }
        lastBall.setLocation(balon);
    }
    
    private void updateInfo(SituacionJugadores sj){
        if ( count == 0 ) {
            count = 1;            
            nGolesMios = nGolesContrario = 0;
        }
        else {
            Point bola=sj.getBola();
            int nXBola = (int)bola.getX();
            boolean esGol = false;
            if (balon.x > 150  && nXBola < 40) {esGol = true; nGolesMios++;}
            if (balon.x < -150 && nXBola > -40){esGol = true; nGolesContrario++;}
            updateInfoBalon(bola,esGol);
            // Cuanto se debe arriesgar
            if ( ( nGolesMios - nGolesContrario ) > 5 ) arriesgar = 1.0;
            else if ( ( nGolesMios - nGolesContrario ) > 1 ) arriesgar = 0.7;
            else if ( nGolesMios == nGolesContrario ) arriesgar = 0.5;
            else if ( ( nGolesMios - nGolesContrario ) < -5 ) arriesgar = 1.0;
            else if ( ( nGolesMios - nGolesContrario ) < -1 ) arriesgar = 0.7;
            count++;
            count %= 1800;
        }
    }
    
    public Vector<Comando> getComandos(SituacionJugadores sj){
        updateInfo(sj);
        Vector<Comando> comandos=new Vector();
        Point contrarios[]=sj.getContrario();
        Point bola=sj.getBola();        
        // Tactica de Caizares
        if ( !ballToGoal ){
            int y=(int)(bola.getY()/2);
            if (y<-30) y=-30;
            if (y>30) y=30;
            comandos.add(new ComandoIrA(0,nValores[0][0], y));
        } else {
            comandos.add(new ComandoIrA(0,nValores[0][0],(int)Y));
        }
        // Marcaje al hombre
        for ( int n = 1 ; n < 11 ; n++ ){
            if ( contrarios[11-n].getX() < lines[n][0] )
                 comandos.add(new ComandoIrA(n,lines[n][0], contrarios[11-n].getY()));
            else if ( contrarios[11-n].getX() > lines[n][1] )
                 comandos.add(new ComandoIrA(n,lines[n][1], contrarios[11-n].getY()));
            else comandos.add(new ComandoIrA(n,contrarios[11-n].getX(), contrarios[11-n].getY()));
        }
        // Ir a por el balon
        int nJug=sj.getMasCercanoDeBola();
        comandos.add(new ComandoIrA(nJug, bola.getX() , bola.getY()));
        
        int masAlejado = (int)(arriesgar*30.0 +.5);
        if ( contrarios[0].getY() > 0.0 ) masAlejado = -masAlejado;
        for (int n=0;n<11;n++)
            comandos.add(new ComandoTiroAPuerta(n,masAlejado));
        return comandos;
    }
}
